package com.jintu.portal.service.datadriver.parser;

import com.alibaba.fastjson.JSONObject;
import com.jintu.portal.service.datadriver.pojo.MsgRespCode;
import com.jintu.portal.service.datadriver.pojo.RespInfo;
import com.jintu.portal.service.datadriver.pojo.Response;
import com.jintu.portal.service.datadriver.pojo.SysRespCode;
import com.jintu.portal.cache.RedisHelper;
import com.jintu.portal.pojo.RetEntity;
import com.jintu.portal.pojo.submit.DataResponse;
import com.jintu.portal.service.datadriver.pojo.entity.SourceData;
import com.jintu.portal.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;

/**
 * @author very
 */
@Slf4j
public class ParserCommon {

    public static boolean isDataSourceIsEmpty(DataResponse dataResponse){
        Response response = new Response();
        response.setRetCode(SysRespCode.QUERY_ERROR.getCode());
        response.setRetDesc(SysRespCode.QUERY_ERROR.getDesc());
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(MsgRespCode.OTHER.getCode());
        respInfo.setCode(MsgRespCode.OTHER.getDesc());
        respInfo.setMessage("{\"Err\": \"源数据为空\"}");
        response.setRetInfo(respInfo);
        dataResponse.setResponseJson(JSONObject.parseObject(JSONObject.toJSONString(response)));
        return false;
    }

    public static void wrapAuthSuccess(DataResponse dataResponse, Response response){
        JSONObject js = new JSONObject();
        js.put("msg", "授权成功");
        if(StringUtils.isNotBlank(((JSONObject) response.getRetInfo().getMessage()).getString("authUrl"))){
            js.put("authUrl", ((JSONObject) response.getRetInfo().getMessage()).getString("authUrl"));
        }
        if(StringUtils.isNotBlank(((JSONObject) response.getRetInfo().getMessage()).getString("tradeNo"))){
            js.put("tradeNo", ((JSONObject) response.getRetInfo().getMessage()).getString("tradeNo"));
        }
        if(StringUtils.isNotBlank(((JSONObject) response.getRetInfo().getMessage()).getString("token"))){
            js.put("token", ((JSONObject) response.getRetInfo().getMessage()).getString("token"));
        }
        dataResponse.setResponseJson(js);
        dataResponse.setRetCode(RetEntity.SUCCESS.getCode());
        dataResponse.setRetMessage(RetEntity.SUCCESS.getMessage());
    }

    public static void wrapSyncDataSuccess(DataResponse dataResponse){
        JSONObject js = new JSONObject();
        js.put("msg", "数据获取成功");
        dataResponse.setResponseJson(js);
        dataResponse.setRetCode(RetEntity.SUCCESS.getCode());
        dataResponse.setRetMessage(RetEntity.SUCCESS.getMessage());
    }

    public static SourceData wrapSourceSavingData(DataResponse dataResponse, Integer userId, Document data){
        SourceData syncData = new SourceData();
        syncData.setId(CommonUtil.genMongoId(dataResponse.getOrderId(), userId));
        syncData.setOrderId(dataResponse.getOrderId());
        syncData.setUserId(userId);
        syncData.setTimestamp(System.currentTimeMillis());
        syncData.setData(data);
        return syncData;
    }

    public static void setResponseSuccessAndCacheTradeNo(DataResponse dataResponse, RedisHelper redisHelper, String tradeNo){
        try {
            dataResponse.setRetCode(RetEntity.SUCCESS.getCode());
            dataResponse.setRetMessage(RetEntity.SUCCESS.getMessage());
            redisHelper.set(dataResponse.getDataSourceCode() + "_" + dataResponse.getOrderId(), tradeNo, null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
